package com.kzd.sdk.core;

import com.kzd.sdk.callback.KZD_JsonCallback;
import com.kzd.sdk.callback.KZD_JsonCallbackAny;
import com.kzd.sdk.callback.KZD_JsonCallbackList;
import com.kzd.sdk.callback.KZD_OnHttpListener;
import com.kzd.sdk.callback.KZD_OnLoginListener;
import com.kzd.sdk.callback.KZD_OnPayListener;
import com.kzd.sdk.callback.KZD_OnRoleListener;
import com.kzd.sdk.entity.KZD_BaseResponse;
import com.kzd.sdk.entity.KZD_BaseResponseAny;
import com.kzd.sdk.entity.KZD_BaseResponseList;
import com.kzd.sdk.entity.KZD_GameCoupon;
import com.kzd.sdk.entity.KZD_GameGift1;
import com.kzd.sdk.entity.KZD_GameGift2;
import com.kzd.sdk.entity.KZD_GameMove;
import com.kzd.sdk.entity.KZD_GameRebate;
import com.kzd.sdk.entity.KZD_GameService;
import com.kzd.sdk.entity.KZD_LoginMain;
import com.kzd.sdk.entity.KZD_LoginSmall;
import com.kzd.sdk.entity.KZD_RechargeCoin;
import com.kzd.sdk.entity.KZD_SdkInitParams;
import com.kzd.sdk.entity.KZD_SdkPayParams;
import com.kzd.sdk.entity.KZD_SdkRoleParams;
import com.kzd.sdk.entity.KZD_ServiceInfo;
import com.kzd.sdk.entity.KZD_SmallGroup;
import com.kzd.sdk.entity.KZD_UserAgree;
import com.kzd.sdk.entity.KZD_UserInfo;
import com.kzd.sdk.error.KZD_CustomException;
import com.kzd.sdk.utils.AppUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.util.List;
import java.util.Locale;

public class KZD_ApiManager {

    private static String getAppId() {
        KZD_SdkInitParams sdkInitParams = KZD_SdkController.getSdkInitParams();
        if (sdkInitParams == null) {
            return "";
        }
        return sdkInitParams.getAppId();
    }

    private static String getLoginKey() {
        KZD_SdkInitParams sdkInitParams = KZD_SdkController.getSdkInitParams();
        if (sdkInitParams == null) {
            return "";
        }
        return sdkInitParams.getLoginKey();
    }

    private static String getUid() {
        KZD_LoginMain loginMain = KZD_SdkController.getLoginMain();
        if (loginMain == null) {
            return "";
        }
        return loginMain.getUid();
    }

    private static String getMemberId() {
        String memberId = KZD_SdkController.getGTMemberId();
        if (memberId == null) {
            return "";
        }
        return memberId;
    }

    /**
     * 主号账号密码登录
     *
     * @param username 账号
     * @param password 密码
     * @param listener 回调
     */
    public static void httpMainAccountLogin(String username, String password, String type, String sessionid, KZD_OnHttpListener<KZD_LoginMain> listener) {
        String time = String.valueOf(System.currentTimeMillis());
        String signStr = username + password + getAppId() + time + getLoginKey();
        String signMd5 = AppUtils.md5(signStr).toLowerCase(Locale.CANADA);
        PostRequest<KZD_BaseResponseAny<KZD_LoginMain>> request = OkGo.<KZD_BaseResponseAny<KZD_LoginMain>>post(KZD_ApiConstance.HTTP_LOGIN_MAIN)
                .params("appid", getAppId())
                .params("member_id", getMemberId())
                .params("user_name", username)
                .params("password", password)
                .params("time", time)
                .params("sign", signMd5);
        if (type != null) {
            request.params("type", type);
        }
        if (sessionid != null) {
            request.params("sessionid", sessionid);
        }
        request.execute(new KZD_JsonCallbackAny<KZD_LoginMain>(KZD_LoginMain.class) {

            @Override
            public void onStart() {
                if (listener != null) {
                    listener.onStart();
                }
            }

            @Override
            public void onSuccess(KZD_LoginMain data) {
                //保存主账号登录信息
                KZD_SdkController.getSp()
                        .put(KZD_ApiConstance.SP_LOGIN_MAIN, data.toString());
                if (listener != null) {
                    listener.onSuccess(data);
                }
            }

            @Override
            public void onError(String message) {
                if (listener != null) {
                    listener.onError(message);
                }
            }
        });
    }

    public static void httpMainAccountLogin(String username, String password, KZD_OnHttpListener<KZD_LoginMain> listener) {
        httpMainAccountLogin(username, password, null, null, listener);
    }

    /**
     * 主号账号一键登录
     *
     * @param uid      用户id
     * @param listener 回调
     */
    public static void httpAppAccountLogin(String uid, KZD_OnHttpListener<KZD_LoginMain> listener) {
        String signStr = uid + getAppId() + getLoginKey();
        String signMd5 = AppUtils.md5(signStr).toLowerCase(Locale.CANADA);
        OkGo.<KZD_BaseResponseAny<KZD_LoginMain>>post(KZD_ApiConstance.HTTP_LOGIN_MAIN_APP)
                .params("appid", getAppId())
                .params("uid", uid)
                .params("sign", signMd5)
                .execute(new KZD_JsonCallbackAny<KZD_LoginMain>(KZD_LoginMain.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(KZD_LoginMain data) {
                        //保存主账号登录信息
                        KZD_SdkController.getSp()
                                .put(KZD_ApiConstance.SP_LOGIN_MAIN, data.toString());
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 小号登录
     *
     * @param smallUid 小号id
     * @param listener 回调
     */
    public static void httpSmallAccountLogin(String smallUid, KZD_OnHttpListener<KZD_LoginSmall> listener) {
        String signStr = getUid() + getAppId() + getLoginKey();
        String signMd5 = AppUtils.md5(signStr).toLowerCase(Locale.CANADA);
        OkGo.<KZD_BaseResponseAny<KZD_LoginSmall>>post(KZD_ApiConstance.HTTP_LOGIN_XIAO)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("small_uid", smallUid)
                .params("sign", signMd5)
                .execute(new KZD_JsonCallbackAny<KZD_LoginSmall>(KZD_LoginSmall.class) {

                    private final KZD_OnLoginListener loginListener =
                            KZD_SdkController.getOnLoginListener();

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(KZD_LoginSmall data) {
                        //保存小号登录信息
                        KZD_SdkController.getSp()
                                .put(KZD_ApiConstance.SP_LOGIN_SMALL, data.toString());
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                        if (loginListener != null) {
                            loginListener.onLoginSuccess(data.getUid(), data.getSessionid());
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                        if (loginListener != null) {
                            loginListener.onLoginError(message);
                        }
                    }
                });
    }

    /**
     * 获取小号列表
     *
     * @param listener 回调
     */
    public static void httpSmallAccountList(KZD_OnHttpListener<List<KZD_SmallGroup>> listener) {
        OkGo.<KZD_BaseResponseList<KZD_SmallGroup>>post(KZD_ApiConstance.HTTP_XIAO_LIST)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackList<KZD_SmallGroup>(KZD_SmallGroup.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(List<KZD_SmallGroup> data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                            listener.onFinish(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                            listener.onFinish(null);
                        }
                    }
                });
    }

    /**
     * 添加小号
     *
     * @param userName 小号名称
     * @param listener 回调
     */
    public static void httpSmallAccountAdd(String userName, KZD_OnHttpListener<String> listener) {
        String signStr = getUid() + getAppId() + getLoginKey();
        String signMd5 = AppUtils.md5(signStr).toLowerCase(Locale.CANADA);
        OkGo.<KZD_BaseResponse>post(KZD_ApiConstance.HTTP_XIAO_ADD)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("user_name", userName)
                .params("sign", signMd5)
                .execute(new KZD_JsonCallback() {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(String data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 获取用户信息
     *
     * @param listener 回调
     */
    public static void httpUserInfo(KZD_OnHttpListener<KZD_UserInfo> listener) {
        OkGo.<KZD_BaseResponseAny<KZD_UserInfo>>post(KZD_ApiConstance.HTTP_USER_INFO)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackAny<KZD_UserInfo>(KZD_UserInfo.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(KZD_UserInfo data) {
                        //保存用户信息
                        if (data.getVerify_info() == 1) {
                            KZD_SdkController.getSp()
                                    .put(KZD_ApiConstance.SP_USER_INFO, data.toString());
                        }
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 游戏角色信息上传
     *
     * @param params   游戏角色信息
     * @param listener 回调
     */
    public static void httpGameRoleUpload(KZD_SdkRoleParams params, KZD_OnHttpListener<String> listener) {
        String signStr = getUid() + getAppId() + getLoginKey();
        String signMd5 = AppUtils.md5(signStr).toLowerCase(Locale.CANADA);
        OkGo.<KZD_BaseResponse>post(KZD_ApiConstance.HTTP_ROLE_UPLOAD)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("small_id", params.getSmallId())
                .params("role_name", params.getRoleName())
                .params("charid", params.getRoleId())
                .params("level", params.getRoleLevel())
                .params("game", params.getGameName())
                .params("serverid", params.getServerName())
                .params("servername", params.getServerName())
                .params("sign", signMd5)
                .execute(new KZD_JsonCallback() {

                    private final KZD_OnRoleListener rolesListener =
                            KZD_SdkController.getOnRolesListener();

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(String data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                            listener.onFinish(data);
                        }
                        if (rolesListener != null) {
                            rolesListener.onRolesSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                            listener.onFinish(null);
                        }
                        if (rolesListener != null) {
                            rolesListener.onRolesError(message);
                        }
                    }
                });
    }


    /**
     * 游戏角色信息上传
     *
     * @param params 游戏角色信息
     */
    public static void httpGameRoleUpload(KZD_SdkRoleParams params) {
        httpGameRoleUpload(params, null);
    }

    /**
     * 获取游戏活动列表
     *
     * @param listener 回调
     */
    public static void httpGameMove(KZD_OnHttpListener<List<KZD_GameMove>> listener) {
        OkGo.<KZD_BaseResponseList<KZD_GameMove>>post(KZD_ApiConstance.HTTP_GAME_ACTIVITY)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackList<KZD_GameMove>(KZD_GameMove.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(List<KZD_GameMove> data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 我的游戏礼包列表
     *
     * @param listener 回调
     */
    public static void httpUserGift(KZD_OnHttpListener<List<KZD_GameGift2>> listener) {
        OkGo.<KZD_BaseResponseList<KZD_GameGift2>>post(KZD_ApiConstance.HTTP_USER_GIFT)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackList<KZD_GameGift2>(KZD_GameGift2.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(List<KZD_GameGift2> data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 获取游戏礼包列表
     *
     * @param listener 回调
     */
    public static void httpGameGift(KZD_OnHttpListener<List<KZD_GameGift1>> listener) {
        OkGo.<KZD_BaseResponseList<KZD_GameGift1>>post(KZD_ApiConstance.HTTP_GAME_GIFT)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackList<KZD_GameGift1>(KZD_GameGift1.class) {
                    @Override
                    public void onSuccess(List<KZD_GameGift1> data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 领取游戏礼包
     *
     * @param listener 回调
     */
    public static void httpDrawGift(String id, KZD_OnHttpListener<String> listener) {
        OkGo.<KZD_BaseResponseAny<String>>post(KZD_ApiConstance.HTTP_DRAW_GIFT)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("id", id)
                .execute(new KZD_JsonCallbackAny<String>(String.class) {
                    @Override
                    public void onSuccess(String data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 获取游戏优惠券列表
     *
     * @param listener 回调
     */
    public static void httpGameCoupon(KZD_OnHttpListener<List<KZD_GameCoupon>> listener) {
        OkGo.<KZD_BaseResponseList<KZD_GameCoupon>>post(KZD_ApiConstance.HTTP_GAME_COUPON)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackList<KZD_GameCoupon>(KZD_GameCoupon.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(List<KZD_GameCoupon> data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 领取游戏优惠券
     *
     * @param listener 回调
     */
    public static void httpDrawCoupon(String id, KZD_OnHttpListener<String> listener) {
        OkGo.<KZD_BaseResponse>post(KZD_ApiConstance.HTTP_DRAW_COUPON)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("id", id)
                .execute(new KZD_JsonCallback() {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(String data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 获取游戏开服表列表
     *
     * @param listener 回调
     */
    public static void httpGameService(KZD_OnHttpListener<KZD_GameService> listener) {
        OkGo.<KZD_BaseResponseAny<KZD_GameService>>post(KZD_ApiConstance.HTTP_GAME_SERVICE)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackAny<KZD_GameService>(KZD_GameService.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(KZD_GameService data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 开服提醒
     *
     * @param listener 回调
     */
    public static void httpRemindService(String id, KZD_OnHttpListener<String> listener) {
        OkGo.<KZD_BaseResponse>post(KZD_ApiConstance.HTTP_REMIND_SERVICE)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("id", id)
                .execute(new KZD_JsonCallback() {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(String data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 设置密码
     *
     * @param password 密码
     * @param listener 回调
     */
    public static void httpSetPassword(String password, KZD_OnHttpListener<String> listener) {
        String signStr = getUid() + getAppId() + getLoginKey();
        String signMd5 = AppUtils.md5(signStr).toLowerCase(Locale.CANADA);
        OkGo.<KZD_BaseResponse>post(KZD_ApiConstance.HTTP_SET_PASSWORD)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("passwd", password)
                .params("sign", signMd5)
                .execute(new KZD_JsonCallback() {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(String data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 修改密码
     *
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @param listener    回调
     */
    public static void httpChangePassword(String oldPassword, String newPassword, KZD_OnHttpListener<String> listener) {
        String signStr = getUid() + getAppId() + getLoginKey();
        String signMd5 = AppUtils.md5(signStr).toLowerCase(Locale.CANADA);
        OkGo.<KZD_BaseResponse>post(KZD_ApiConstance.HTTP_CHANGE_PASSWORD)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("oldpasswd", oldPassword)
                .params("passwd", newPassword)
                .params("sign", signMd5)
                .execute(new KZD_JsonCallback() {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(String data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 实名认证
     *
     * @param cardName 名字
     * @param cardCode 身份证号
     * @param listener 回调
     */
    public static void httpVerifyName(String cardName, String cardCode, KZD_OnHttpListener<KZD_UserInfo> listener) {
        String signStr = getUid() + getAppId() + getLoginKey();
        String signMd5 = AppUtils.md5(signStr).toLowerCase(Locale.CANADA);
        OkGo.<KZD_BaseResponseAny<KZD_UserInfo>>post(KZD_ApiConstance.HTTP_VERIFY_NAME)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("name", cardName)
                .params("idcard", cardCode)
                .params("sign", signMd5)
                .execute(new KZD_JsonCallbackAny<KZD_UserInfo>(KZD_UserInfo.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(KZD_UserInfo data) {
                        //保存用户信息
                        KZD_SdkController.getSp()
                                .put(KZD_ApiConstance.SP_USER_INFO, data.toString());
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 获取手机验证码
     *
     * @param phone    手机号
     * @param listener 回调
     */
    public static void httpPhoneCode(String phone, int type, KZD_OnHttpListener<String> listener) {
        OkGo.<KZD_BaseResponseAny<String>>post(KZD_ApiConstance.HTTP_PHONE_CODE)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("phone", phone)
                .params("type", type + "")
                .execute(new KZD_JsonCallbackAny<String>(String.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(String data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 绑定手机
     *
     * @param phone     手机号
     * @param sessionid 会话id
     * @param code      验证码
     * @param listener  回调
     */
    public static void httpPhoneBind(String phone, String sessionid, String code, KZD_OnHttpListener<KZD_UserInfo> listener) {
        String signStr = getUid() + getAppId() + getLoginKey();
        String signMd5 = AppUtils.md5(signStr).toLowerCase(Locale.CANADA);
        OkGo.<KZD_BaseResponseAny<KZD_UserInfo>>post(KZD_ApiConstance.HTTP_PHONE_BIND)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("phone", phone)
                .params("sessionid", sessionid)
                .params("code", code)
                .params("sign", signMd5)
                .execute(new KZD_JsonCallbackAny<KZD_UserInfo>(KZD_UserInfo.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(KZD_UserInfo data) {
                        //保存用户信息
                        KZD_SdkController.getSp()
                                .put(KZD_ApiConstance.SP_USER_INFO, data.toString());
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 获取客服信息
     *
     * @param listener 回调
     */
    public static void httpServiceInfo(KZD_OnHttpListener<KZD_ServiceInfo> listener) {
        OkGo.<KZD_BaseResponseAny<KZD_ServiceInfo>>post(KZD_ApiConstance.HTTP_SERVICE_INFO)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackAny<KZD_ServiceInfo>(KZD_ServiceInfo.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(KZD_ServiceInfo data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }


    /**
     * 充值面额数据
     */
    public static void httpRechargeCoin(KZD_OnHttpListener<KZD_RechargeCoin> listener) {
        OkGo.<KZD_BaseResponseAny<KZD_RechargeCoin>>post(KZD_ApiConstance.HTTP_COIN_RECHARGE)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackAny<KZD_RechargeCoin>(KZD_RechargeCoin.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(KZD_RechargeCoin data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }


    /**
     * 创建充值订单
     */
    public static void httpRechargeOrder(String id, String money, String pay_type, KZD_OnHttpListener<String> listener) {
        PostRequest<KZD_BaseResponseAny<String>> request = OkGo.<KZD_BaseResponseAny<String>>post(KZD_ApiConstance.HTTP_ORDER_RECHARGE)
                .params("appid", getAppId())
                .params("uid", getUid())
                .params("pay_type", pay_type);
        if (id != null) {
            request.params("id", id);
        }
        if (id != null) {
            request.params("money", money);
        }
        request.execute(new KZD_JsonCallbackAny<String>(String.class) {

            @Override
            public void onStart() {
                if (listener != null) {
                    listener.onStart();
                }
            }

            @Override
            public void onSuccess(String data) {
                if (listener != null) {
                    listener.onSuccess(data);
                }
            }

            @Override
            public void onError(String message) {
                if (listener != null) {
                    listener.onError(message);
                }
            }
        });
    }

    public static void httpGamePayLink(KZD_SdkPayParams payParams, KZD_OnHttpListener<String> listener) {
        String sign = payParams.getSmallId() + getAppId() + payParams.getServerId() +
                payParams.getMoney();
        String url = KZD_ApiConstance.HTTP_HOST + "/?ct=h5pay&uid=" +
                payParams.getSmallId() + "&appid=" +
                getAppId() + "&cporderid=" +
                payParams.getCpOrderId() + "&serverid=" +
                payParams.getServerId() + "&extinfo=" +
                payParams.getExtInfo() + "&amount=" +
                payParams.getMoney() + "&sign=" +
                AppUtils.md5(sign) + "&xiaohao=1&charid=" +
                payParams.getRoleId();
        if (listener != null) {
            listener.onSuccess(url);
        }
    }

    /**
     * 订单查询
     */
    public static void httpGamePayOrder(String orderId) {
        OkGo.<KZD_BaseResponse>post(KZD_ApiConstance.HTTP_ORDER_INQUIRE)
                .params("oid", orderId)
                .execute(new KZD_JsonCallback() {

                    private final KZD_OnPayListener payListener =
                            KZD_SdkController.getOnPayListener();

                    @Override
                    public void onSuccess(String data) {
                        if (payListener != null) {
                            payListener.onPayingSuccess(data);
                        }
                    }

                    @Override
                    public void onError(Response<KZD_BaseResponse> response) {
                        Throwable throwable = response.getException();
                        if (throwable instanceof KZD_CustomException) {
                            if (payListener != null) {
                                payListener.onPayingError(throwable.getMessage());
                            }
                        }
                        super.onError(response);
                    }
                });
    }


    /**
     * 用户注册
     *
     * @param username 账号
     * @param password 密码
     * @param listener 回调
     */
    public static void httpUserRegister(String username, String password, String code, String sessionid, String type, String name, String idcard, KZD_OnHttpListener<KZD_LoginMain> listener) {
        PostRequest<KZD_BaseResponseAny<KZD_LoginMain>> request = OkGo.<KZD_BaseResponseAny<KZD_LoginMain>>post(KZD_ApiConstance.HTTP_USER_REGISTER)
                .params("appid", getAppId())
                .params("member_id", getMemberId())
                .params("user_name", username)
                .params("passwd", password)
                .params("idcard", idcard)
                .params("name", name)
                .params("type", type);
        if (code != null) {
            request.params("code", code);
        }
        if (sessionid != null) {
            request.params("sessionid", sessionid);
        }
        request.execute(new KZD_JsonCallbackAny<KZD_LoginMain>(KZD_LoginMain.class) {

            @Override
            public void onStart() {
                if (listener != null) {
                    listener.onStart();
                }
            }

            @Override
            public void onSuccess(KZD_LoginMain data) {
                KZD_SdkController.getSp()
                        .put(KZD_ApiConstance.SP_LOGIN_MAIN, data.toString());
                if (listener != null) {
                    listener.onSuccess(data);
                }
            }

            @Override
            public void onError(String message) {
                if (listener != null) {
                    listener.onError(message);
                }
            }
        });
    }


    /**
     * 忘记密码
     *
     * @param password 密码
     * @param listener 回调
     */
    public static void httpUserForget(String phone, String password, String code, String sessionid, KZD_OnHttpListener<String> listener) {
        PostRequest<KZD_BaseResponseAny<String>> request = OkGo.<KZD_BaseResponseAny<String>>post(KZD_ApiConstance.HTTP_USER_FORGET)
                .params("appid", getAppId())
                .params("phone", phone)
                .params("passwd", password)
                .params("code", code)
                .params("sessionid", sessionid);
        request.execute(new KZD_JsonCallbackAny<String>(String.class) {

            @Override
            public void onStart() {
                if (listener != null) {
                    listener.onStart();
                }
            }

            @Override
            public void onSuccess(String data) {
                if (listener != null) {
                    listener.onSuccess(data);
                }
            }

            @Override
            public void onError(String message) {
                if (listener != null) {
                    listener.onError(message);
                }
            }
        });
    }


    /**
     * 协议数据
     */
    public static void httpUserAgree(String type, KZD_OnHttpListener<KZD_UserAgree> listener) {
        OkGo.<KZD_BaseResponseAny<KZD_UserAgree>>post(KZD_ApiConstance.HTTP_USER_AGREE)
                .params("appid", getAppId())
                .params("type", type)
                .execute(new KZD_JsonCallbackAny<KZD_UserAgree>(KZD_UserAgree.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(KZD_UserAgree data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }


    /**
     * 申请返利列表
     */
    public static void httpRebateApplyList(KZD_OnHttpListener<KZD_GameRebate> listener) {
        OkGo.<KZD_BaseResponseAny<KZD_GameRebate>>post(KZD_ApiConstance.HTTP_REBATE_APPLY_LIST)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackAny<KZD_GameRebate>(KZD_GameRebate.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(KZD_GameRebate data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }

    /**
     * 申请返利记录
     */
    public static void httpRebateApplyRecord(KZD_OnHttpListener<List<KZD_GameRebate.List>> listener) {
        OkGo.<KZD_BaseResponseList<KZD_GameRebate.List>>post(KZD_ApiConstance.HTTP_REBATE_APPLY_RECODE)
                .params("appid", getAppId())
                .params("uid", getUid())
                .execute(new KZD_JsonCallbackList<KZD_GameRebate.List>(KZD_GameRebate.List.class) {

                    @Override
                    public void onStart() {
                        if (listener != null) {
                            listener.onStart();
                        }
                    }

                    @Override
                    public void onSuccess(List<KZD_GameRebate.List> data) {
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public void onError(String message) {
                        if (listener != null) {
                            listener.onError(message);
                        }
                    }
                });
    }
}
